package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.com.utt.unms.pojo.AccountConfig;

@Mapper
public interface AccountConfigMapper {
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM account",
	    "WHERE user_id=#{userId}",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM account ",
	    "WHERE user_id=#{userId}",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<AccountConfig> queryByCondition(Map<String, Object> conditions);
	
	@Insert("insert into account(user_id,username,password,remark,create_time,end_time,start_time,max_occurs) values(#{userId},#{username},#{password},#{remark},#{createTime},#{endTime},#{startTime},#{maxOccurs})")
	void addAccountConfig(AccountConfig accountConfig);
	@Update("update account set password=#{password},remark=#{remark},end_time=#{endTime},start_time=#{startTime},max_occurs=#{maxOccurs} where id=#{id}")
	void editAccountConfig(AccountConfig accountConfig);
	@Delete({"<script>",
	    "DELETE FROM ", 
	    "account ",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void delAccountConfig(@Param("ids") List<Long> ids);
	
	@Select("select * from account where user_id=#{userId}")
	List<AccountConfig> queryByUserId(@Param("userId") Long userId);
	
	@Insert({"<script>",
			 "insert into account(user_id, username, password,remark,create_time,end_time,start_time,max_occurs) values ",
			 "<foreach collection='accountConfigLists' item='item' index='index' separator=','>",
			 "(#{item.userId},#{item.username},#{item.password},#{item.remark},#{item.createTime},#{item.endTime},#{item.startTime},#{item.maxOccurs})",
			 "</foreach>",
			"</script>"})
	void batchSave(@Param("accountConfigLists")List<AccountConfig> accountConfigLists);
	
	@Select("select * from account where username=#{username}")
	AccountConfig queryByName(@Param("username") String username);

}
